import Vue from 'vue';
import formatDate from '@/utils/date';

// 注册
Vue.filter('compare-list', (value, list) => {
  let name = value;
  const length = list.length;
  if (!length || (!value && value !== 0)) {
    return value;
  } else {
    for (let i = 0; i < length; i++) {
      const item = list[i];
      if (value == item.value) {
        name = item.name;
        break;
      }
    }
  }

  return name;
});

Vue.filter('compare-arr', (arr, list) => {
  let name = '';
  const alength = arr && arr.length;
  const llength = list && list.length;
  if (alength && llength) {
    for (let i = 0; i < alength; i++) {
      for (let j = 0; j < llength; j++) {
        if (arr[i] === list[j].value) {
          if (name) {
            name = `${name}、${list[j].name}`;
          } else {
            name = `${list[j].name}`;
          }
          break;
        }
      }
    }
  }

  if (name.length) {
    return name;
  } else {
    return arr;
  }
});

Vue.filter('date-locale', (date) => {
  if (!date) {
    return date;
  } else if (date && typeof date === 'object') {
    return date.toLocaleDateString();
  } else {
    return formatDate(new Date(date), 'yyyy-MM-dd');
  }
});

Vue.filter('date-format', (date) => {
  if (!date) {
    return date;
  } else {
    return formatDate(new Date(date), 'yyyy-MM-dd hh:mm:ss');
  }
});

Vue.filter('day-format', (date) => {
  if (!date) {
    return date;
  } else {
    return formatDate(new Date(date), 'yyyy-MM-dd');
  }
});
